package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.HandCarDao;
import cn.hls.entity.HandCar;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;

 
/**
 *@fileName: HandCarDaoImpl.java
 *@package: cn.christ.dao.impl
 *@Description:  车辆基本信息数据接口实现类
 *@author: yinghui.zhang
 *@email: uuhui@163.com
 *@date: 2015-7-18
 */
@Component("handCarDao")
public class HandCarDaoImpl extends BaseDaoImpl<HandCar> implements HandCarDao {

	@Override
	public PageInfo<HandCar> loadHandCarInfo(HandCar entity, PageInfo<HandCar> page) throws Exception {
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from HandCar p left join fetch p.car c left join fetch c.carCategory cc ");
		selectHql.append("left join fetch c.company com left join fetch c.driver dr left join fetch c.escort es ");
		// 定义汇总Hql语句
		StringBuilder counthql = new StringBuilder("select count(p.id) from HandCar p left join p.car c left join c.carCategory cc ");
		counthql.append("left join c.company com left join c.driver dr left join c.escort es ");
		//定义查询条件hql
		StringBuilder conditionHql=new StringBuilder("where p.deleted=? ");
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//根据承运企业编号查询
			if(entity.getCompanyId()!=null){
				conditionHql.append("and p.companyId=? ");
				paramList.add(entity.getCompanyId());
			}
			//按挂车牌号查询
			 if(StringUtils.hasText(entity.getLicenseNum())){
				 conditionHql.append("and p.licenseNum like ? ");
				 paramList.add("%"+entity.getLicenseNum().trim()+"%");
			 }
			 //按主车信息查询
			 if(entity.getCar()!=null){
				 //按车辆编号查询
				 if(entity.getId()!=null){
					 conditionHql.append("and c.id=? ");
					 paramList.add(entity.getCar().getId());
				 }
				 //按车牌号查询
				 if(StringUtils.hasText(entity.getCar().getLicenseNum())){
					 conditionHql.append("and c.licenseNum like ? ");
					 paramList.add("%"+entity.getCar().getLicenseNum().trim()+"%");
				 }
			 }
			 //按车身颜色查询
			if(StringUtils.hasText(entity.getColor())){
				 conditionHql.append("and p.color=? ");
				 paramList.add(entity.getColor());
			 }
			 //按车轮数查询
			 if(entity.getWheelNum()!=null){
				 conditionHql.append("and p.wheelNum=? ");
				 paramList.add(entity.getWheelNum());
			 }
			   
			 //载重量
			 if(entity.getCarryCapacity()!=null){
				 conditionHql.append("and p.carryCapacity=? ");
				 paramList.add(entity.getCarryCapacity());
			 }
			 //按总质量查询
			 if(entity.getTotalMass()!=null){
				 conditionHql.append("and p.totalMass=? ");
				 paramList.add(entity.getTotalMass());
			 }
		}
		if(conditionHql.length()>0){
			counthql. append(conditionHql);
			selectHql.append(conditionHql);
		}
		selectHql.append(" order by p.id desc ");
		return find(selectHql.toString(), counthql.toString(), paramList.toArray(), page);
	}
	
	@Override
	public void delete(Object entityId) throws Exception {
		String []ids=(String[])entityId;
		StringBuilder hql=new StringBuilder("update HandCar set deleted=? where id in (");
		List<Object>params=new ArrayList<Object>();
		params.add(true);
		for (String id:ids){
			hql.append("?,");
			params.add(Long.valueOf(id));
		}	
		hql.delete(hql.length()-1, hql.length());
		hql.append(")");
		 try {
			executeHQLUpdate(hql.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.UPDATE_ERROR,"更新挂车信息失败" );
		}
	}

	 
	 
	@Override
		public boolean exists(HandCar entity)throws Exception{
			StringBuilder selectHql=new StringBuilder("select count(id) from HandCar where deleted=? ");
			List<Object>params=new ArrayList<Object>();
			params.add(false);
			if(entity!=null){
				if(StringUtils.hasText(entity.getLicenseNum())){//姓名
					selectHql.append("and licenseNum=? ");
					params.add(entity.getLicenseNum().trim());
				}
				if(entity.getId()!=null){
					selectHql.append("and id<>? ");
					params.add(entity.getId());
				}
			}
			Query query=this.getSession().createQuery(selectHql.toString());
			for(int i=0;i<params.size();i++){
				query.setParameter(i, params.get(i));
			}
			Long c = Long.valueOf(((Number) query.uniqueResult()).longValue());
			return c.intValue()>0?true:false;
	}

	@Override
	public List<HandCar> loadHandCarList(HandCar entity) throws Exception {
		StringBuilder hql=new StringBuilder("from HandCar p inner join fetch p.car c  ");
		List<Object>paramList=new ArrayList<Object>();
		if (entity != null) {
			//按车牌号
			 if(StringUtils.hasText(entity.getLicenseNum())){
				 hql.append("and p.licenseNum like ? ");
				 paramList.add("%"+entity.getLicenseNum().trim()+"%");
			 }
			 //按主车信息查询
			 if(entity.getCar()!=null){
				 //按车牌号查询
				 if(StringUtils.hasText(entity.getCar().getLicenseNum())){
					 hql.append("and c.licenseNum like ?  ");
					 paramList.add("%"+entity.getLicenseNum().trim()+"%");
				 }
				 //按主车编号查询
				 if(entity.getCar().getId()!=null){
					 hql.append("and c.id=? ");
					 paramList.add(entity.getCar().getId());
				 }
			 }
		}
		return findByHql(hql.toString(), paramList.toArray());
	}

	@SuppressWarnings("unchecked")
	@Override
	public HandCar loadHandCarByLicenseNum(String licenseNum) throws Exception {
		String hql="from HandCar p where deleted=? and p.licenseNum=?";
		List<HandCar>list=(List<HandCar>)find(hql, new Object[]{false,licenseNum});
		if(list==null ||list.isEmpty()){
			return null;
		}else{
			return list.get(0);
		}
	}
	
}
